package com.olt.server.shiro;
//权限验证服务
import com.olt.dao.user.UserDao;
import com.olt.domain.user.OltUser;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.SM3Util;

import java.util.Arrays;

@Service
public class MyReaml extends AuthorizingRealm {
    @Autowired
    UserDao userDao;

    //    授权


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }
    //    认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String userName=token.getUsername();
        String password= String.valueOf(token.getPassword());
        OltUser user=userDao.getFirstByUserNameAndHashPassword(userName,password);
        if (user == null){
            throw new UnknownAccountException("没有该用户");
        }

        return new SimpleAuthenticationInfo(user,user.getHashPassword(),getName());
    }


}
